﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using AmiEmuLib.Configuration.FiltersConfig;
using System.Xml;
using TomanuExtensions;
using TomanuExtensions.Utils;

namespace AmiEmuLib.AmigaConfiguration.DisplayConfig
{
    /// <summary>
    /// Display settings. Releted to host.
    /// </summary>
    public class Display : XmlBase
    {
        /// <summary>
        /// Settings for full screen.
        /// </summary>
        public Fullscreen Fullscreen;

        /// <summary>
        /// Windowed settings.
        /// </summary>
        public Window Window;

        /// <summary>
        /// Should be between 1..50
        /// </summary>
        public int RenderEveryNFrame;

        /// <summary>
        /// Buffering mode.
        /// </summary>
        public BufferingMode Buffering;

        /// <summary>
        /// Render in windowed or fullscreen.
        /// </summary>
        public bool FullscreenActive;

        /// <summary>
        /// Smart centering. 
        /// </summary>
        public bool SmartCentering;

        /// <summary>
        /// Filter name.
        /// </summary>
        public string FilterName;

        /// <summary>
        /// Should be between -1..1
        /// </summary>
        public float Contrast;

        /// <summary>
        /// Should be between -1..1
        /// </summary>
        public float Brightness;

        /// <summary>
        /// Should be between 0..2
        /// </summary>
        public float Gamma;

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="a_reader"></param>
        public Display(XmlReader a_reader)
            : base(a_reader)
        {
        }

        protected override void ReadXml(XmlReader a_reader)
        {
            a_reader.ReadStartElement(GetType().Name);

            Fullscreen = new Fullscreen(a_reader);
            Window = new Window(a_reader);
            RenderEveryNFrame = a_reader.ReadElementContentAsInt("RenderEveryNFrame");
            Buffering = a_reader.ReadElementContentAsEnum<BufferingMode>("Buffering");
            FullscreenActive = a_reader.ReadElementContentAsBoolean("FullscreenActive");
            SmartCentering = a_reader.ReadElementContentAsBoolean("SmartCentering");
            FilterName = a_reader.ReadElementContentAsString("FilterName");
            Contrast = a_reader.ReadElementContentAsInt("Contrast");
            Brightness = a_reader.ReadElementContentAsInt("Brightness");
            Gamma = a_reader.ReadElementContentAsInt("Gamma");

            a_reader.ReadEndElement();
        }

        public override void WriteXml(XmlWriter a_writer)
        {
            a_writer.WriteStartElement(GetType().Name);

            Fullscreen.WriteXml(a_writer);
            Window.WriteXml(a_writer);
            a_writer.WriteElement("RenderEveryNFrame", RenderEveryNFrame);
            a_writer.WriteElement("Buffering", Buffering);
            a_writer.WriteElement("FullscreenActive", FullscreenActive);
            a_writer.WriteElement("SmartCentering", SmartCentering);
            a_writer.WriteElement("FilterName", FilterName);
            a_writer.WriteElement("Contrast", Contrast);
            a_writer.WriteElement("Brightness", Brightness);
            a_writer.WriteElement("Gamma", Gamma);

            a_writer.WriteEndElement();
        }
    }
}
